﻿namespace TuxedoLib
{
	public static class Draw
	{
		public static int CreateColor(byte red, byte green, byte blue, byte alpha)
		{
			return ((int)alpha << 24) | ((int)red << 16) | ((int)green << 8) | ((int)blue);
		}

		public static int CreateColor(byte red, byte green, byte blue)
		{
			return (255 << 24) | ((int)red << 16) | ((int)green << 8) | ((int)blue);
		}

		public static void RectangleFill(Image image, int color, int left, int top, int width, int height)
		{
			int right = left + width - 1;
			int bottom = top + height - 1;
			int imageWidth = image.width;
			int imageHeight = image.height;
			if (right >= imageWidth) right = imageWidth - 1;
			if (bottom >= imageHeight) bottom = imageHeight - 1;
			if (left < 0) left = 0;
			if (top < 0) top = 0;
			int x, y, index;
			int actualWidth = right - left + 1;
			int[] pixels = image.pixels;
			// TODO: check color for alpha and do mixing in a separate loop.
			// Keep this loop for solid colors to keep it fast for 90% of cases.
			for (y = top; y <= bottom; ++y)
			{
				index = right + y * imageWidth;
				for (x = actualWidth; x > 0; --x)
				{
					pixels[index--] = color;
				}
			}
		}

		public static void SetPixel(Image image, int color, int x, int y)
		{
			if (x >= 0 && y >= 0 && x < image.width && y < image.height)
			{
				image.pixels[image.width * y + x] = color;
			}
		}
	}
}
